/*
* @Author: Josh
* @Date:   2019-02-01 11:08:32
* @Last Modified by:   Administrator
* @Last Modified time: 2019-05-02 21:15:05
*/
'use strict';

require('page/common/nav/index.js');
require('page/common/header/index.js');
require('./index.css');
var navSide       = require('page/common/nav-side/index.js'),
    _mall         = require('util/mall.js'),
    _order        = require('service/order-service.js'),
    templateIndex = require('./index.string');

//page 逻辑部分
var page = {
    data : {
        orderNumber : _mall.getUrlParam('orderNumber')
    },
    init : function(){
        this.onLoad();
        this.bindEvent();
    },
    onLoad : function(){
        //初始化左侧菜单
        navSide.init({
            name : 'order-list'
        });
        //加载商品详情数据
        this.loadDetail();
    },
    bindEvent : function(){
        var _this = this;
        $(document).on('click', '.order-cancel', function(){
            if(window.confirm('确定要取消该订单吗？')){
                _order.cancelOrder(_this.data.orderNumber, function(res){
                    _mall.successTips('该订单取消成功！');
                    _this.loadDetail();
                }, function(errMsg){
                    _mall.errorTips(errMsg);
                })
            }
        });
    },
    //加载订单列表
    loadDetail : function(){
        var orderDetailHtml = '',
            _this           = this,
            $content        = $('.content');

        $content.html('<div class="loading"></div>');
        _order.getOrderDetail(this.data.orderNumber, function(res){
            
            _this.dataFilter(res);
            // 渲染html
            orderDetailHtml = _mall.renderHtml(templateIndex, res);
            $content.html(orderDetailHtml);
        }, function(errMsg){
            $content.html('<p class="err-tip">'+ errMsg +'</p>')
        });
    },
    //数据的适配
    dataFilter : function(data){
        //判断是否已经支付
        data.needPay        = data.status == 10;
        data.isCancelable   = data.status == 10;

    }
};
    
$(function(){
    page.init(); 
});